//
//  SINACustomPickView.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/22.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "SINAServiceTimePickView.h"

@interface SINAServiceTimePickView()<UIPickerViewDataSource, UIPickerViewDelegate>
/** 1.选中的字符串 */
@property (nonatomic, strong, nullable)NSString *selectedTitle;

@property (nonatomic, strong)UILabel *startLabel;
@property (nonatomic, strong)UILabel *endLabel;

@property (nonatomic, strong)UIView *centerView;


//开始时间
@property (nonatomic, strong, nullable)NSString *startHour;
@property (nonatomic, strong, nullable)NSMutableArray *startHourArray;
@property (nonatomic, strong, nullable)NSString *startMin;
@property (nonatomic, strong, nullable)NSMutableArray *startMinArray;

//结束时间
@property (nonatomic, strong, nullable)NSString *endHour;
@property (nonatomic, strong, nullable)NSMutableArray *endHourArray;
@property (nonatomic, strong, nullable)NSString *endMin;
@property (nonatomic, strong, nullable)NSMutableArray *endMinArray;


@end

@implementation SINAServiceTimePickView

@dynamic delegate;

- (void)initPickView
{
    [super initPickView];
    
    _dataArray=[NSMutableArray mutableCopy];
    
    
    
    self.pickerView.frame=CGRectMake(10,  54, self.contentView.frame.size.width-20, self.contentView.frame.size.height-80);
    self.diviLabel.frame=CGRectMake(11, self.contentView.frame.size.height/2-12, 5, 51);
    [self.pickerView setDelegate:self];
    [self.pickerView setDataSource:self];
    
    self.startLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width/4+self.frame.size.width/4/3/3, self.contentView.frame.size.height/2-12, self.frame.size.width/4/3, 51)];
    
    self.startLabel.backgroundColor=[UIColor clearColor];
    self.startLabel.text=@":";
    self.startLabel.font=[UIFont systemFontOfSize:22];
    [self.contentView addSubview:self.startLabel];
    
    
    self.endLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width/4*3, self.contentView.frame.size.height/2-12, self.frame.size.width/4/3, 51)];
    self.endLabel.text=@":";
    self.endLabel.font=[UIFont systemFontOfSize:22];
    self.endLabel.backgroundColor=[UIColor clearColor];
    
    [self.contentView addSubview:self.endLabel];
    
    
    self.centerView = [[UIView alloc]initWithFrame:CGRectMake(self.contentView.frame.size.width/2-12, self.contentView.frame.size.height/2-12+25, 30, 1)];
    self.centerView.backgroundColor=[UIColor grayColor];
    
    [self.contentView addSubview:self.centerView];
    
    
    
    self.startHour = @"00";
    self.startMin = @"00";
    self.endHour = @"00";
    self.endMin = @"00";
    
    self.startHourArray = [[NSMutableArray alloc]init];
    self.endHourArray = [[NSMutableArray alloc]init];
    
    self.startMinArray = [[NSMutableArray alloc]init];
    self.endMinArray = [[NSMutableArray alloc]init];
    
    for (NSInteger i=0; i<24; i++) {
        [self.startHourArray addObject:[NSString stringWithFormat:@"%ld",(long)i]];
        [self.endHourArray addObject:[NSString stringWithFormat:@"%ld",(long)i]];
    }
    
    for (NSInteger i=0; i<60; i++) {
        [self.startMinArray addObject:[NSString stringWithFormat:@"%ld",(long)i]];
        [self.endMinArray addObject:[NSString stringWithFormat:@"%ld",(long)i]];
    }
    
}

-(void)setDefalutSelectRowStr:(NSString *)defalutSelectRowStr{
    _defalutSelectRowStr=defalutSelectRowStr;
    
    for (NSInteger i = 0; i<_dataArray.count; i++) {
        
        
        if ([_dataArray[i] isEqualToString:_defalutSelectRowStr]) {
            [self.pickerView reloadAllComponents];
            [self.pickerView selectRow:i inComponent:0 animated:NO];
            
        }
    }
}

// 返回多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 4;
}
// 返回每列的行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component==0) {
        return  self.startHourArray.count;
    }else if (component==1) {
        return  self.startMinArray.count;
    }else if (component==2) {
        return  self.endHourArray.count;
    }else  {
        return  self.endMinArray.count;
    }
}
// 返回pickerView的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 48;
}
// 反回pickerView的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    
    if (component==0) {
        return self.frame.size.width/4;
    }else if (component==1) {
        return self.frame.size.width/4;
    }else if (component==2) {
        return self.frame.size.width/4;
    }else  {
        return self.frame.size.width/4;
    }
    
}
// 返回pickerView 每行的内容
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *rowString =[NSString stringWithFormat:@"%ld",row];
    if (row<10) {
        rowString =[NSString stringWithFormat:@"0%ld",row];
    }
    //self.selectedTitle = self.dataArray[row];
    if (component==0) {
        rowString=self.startHourArray[row];
        if ([rowString integerValue]<10) {
            rowString =[NSString stringWithFormat:@"0%@",rowString];
        }
        [self.endHourArray removeAllObjects];
        for (NSInteger i=[rowString integerValue]; i<24; i++) {
            [self.endHourArray addObject:[NSString stringWithFormat:@"%ld",i]];
        }
        [self.pickerView reloadComponent:2];
        //开始 小时
        self.startHour=rowString;
    }else if (component==1) {
        rowString=self.startMinArray[row];
        if ([rowString integerValue]<10) {
            rowString =[NSString stringWithFormat:@"0%@",rowString];
        }
        [self.endMinArray removeAllObjects];
        if([rowString integerValue]>=59){
            for (NSInteger i=0; i<60; i++) {
                [self.endMinArray addObject:[NSString stringWithFormat:@"%ld",i]];
            }
            NSInteger row=[self.pickerView selectedRowInComponent:2];
            if(row+1<self.endHourArray.count-1){
                [self.pickerView selectRow: row+1 inComponent:2 animated:YES];
            }else{
                [self.pickerView selectRow: self.endMinArray.count-1 inComponent:3 animated:YES];
            }
        }else{
            for (NSInteger i=[rowString integerValue]+1; i<60; i++) {
                [self.endMinArray addObject:[NSString stringWithFormat:@"%ld",i]];
            }
        }
        [self.pickerView reloadComponent:3];
        //开始 分钟
        self.startMin=rowString;
    }else if (component==2) {
        //结束 小时
        rowString=self.endHourArray[row];
        if ([rowString integerValue]<10) {
            rowString =[NSString stringWithFormat:@"0%@",rowString];
        }
        self.endHour=rowString;
    }else  {
        
        rowString=self.endMinArray[row];
        if ([rowString integerValue]<10) {
            rowString =[NSString stringWithFormat:@"0%@",rowString];
        }
        //结束 分钟
        self.endMin=rowString;
    }
}
// 返回pickerView 每行的view
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view
{
    
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor =[UIColor grayColor];
        }
    }
    
    
    
    UIView *rowView = [[UIView alloc]init];
    
    UILabel *label = [[UILabel alloc]init];
    label.font=[UIFont systemFontOfSize:18];
    label.textAlignment = 1;
    
    if (component==0) {
        rowView.backgroundColor = [UIColor clearColor];
        label.frame=CGRectMake(self.frame.size.width/4/2, 0, self.frame.size.width/4/2, 48);
        label.backgroundColor=[UIColor clearColor];
        [label setTextAlignment:NSTextAlignmentCenter];
        
        NSString * rowString=self.startHourArray[row];
        if ([rowString integerValue]<10) {
            rowString =[NSString stringWithFormat:@"0%@",rowString];
        }
        
        label.text=rowString;
    }else if (component==1) {
        NSString * rowString=self.startMinArray[row];
        if ([rowString integerValue]<10) {
            rowString =[NSString stringWithFormat:@"0%@",rowString];
        }
        
        label.text=rowString;
        
        label.backgroundColor=[UIColor clearColor];
        [label setTextAlignment:NSTextAlignmentLeft];
        label.frame=CGRectMake(self.frame.size.width/4/4, 0, self.frame.size.width/4-self.frame.size.width/4/4, 48);
    }else if (component==2) {
        label.frame=CGRectMake(self.frame.size.width/4/4, 0, self.frame.size.width/4/2, 48);
        label.backgroundColor=[UIColor clearColor];
        [label setTextAlignment:NSTextAlignmentCenter];
        
        NSString * rowString=self.endHourArray[row];
        if ([rowString integerValue]<10) {
            rowString =[NSString stringWithFormat:@"0%@",rowString];
        }
        label.text=rowString;
    }else  {
        NSString * rowString=self.endMinArray[row];
        if ([rowString integerValue]<10) {
            rowString =[NSString stringWithFormat:@"0%@",rowString];
        }
        
        label.text=rowString;
        
        label.backgroundColor=[UIColor clearColor];
        [label setTextAlignment:NSTextAlignmentLeft];
        label.frame=CGRectMake(0, 0, self.frame.size.width/4-self.frame.size.width/4/4, 48);
    }
    
    
    
    
    
    
    [rowView addSubview:label];
    
    return rowView;
    
    
    
}
#pragma mark -点击确定按钮
- (void)clickConfirmButton
{
    
    [self.delegate serviceTimeSelectPickView:self startHour:self.startHour startMin:self.startMin endHour:self.endHour endMin:self.endMin];
    [super clickConfirmButton];
}
-(void)clickCancelButton{
    [self.delegate dismissServiceTimeSelectPickView:self];
    [super clickCancelButton];
}

- (void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    _selectedTitle = dataArray.firstObject;
    [self.pickerView reloadAllComponents];
    
}


@end
